package window

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"translate/config"
)

func SetConfig(w *fyne.Window) {
	win := *w
	//更改大小
	win.Resize(fyne.NewSize(450, 300))
	//设置固定大小
	//win.SetFixedSize(true)

	//创建内容
	content := makeConfigContent(w)

	//设置内容
	win.SetContent(content)
}

func makeConfigContent(w *fyne.Window) *fyne.Container {

	return container.NewBorder(
		makeTopContainer(w),
		nil, nil, nil,
		container.NewPadded(
			makeBottomContainer(),
		),
	)
}

// 创建顶部容器
func makeTopContainer(w *fyne.Window) *fyne.Container {

	//点击保存
	saveBtn := widget.NewButtonWithIcon("保存", theme.DocumentSaveIcon(), func() {
		//保存配置文件
		config.SaveConfigFile()
		dialog.ShowInformation("保存成功", "配置保存成功", *w)
	})

	return container.NewBorder(
		nil,
		nil,
		nil,
		saveBtn,
	)
}

// 创建底部容器
func makeBottomContainer() *container.AppTabs {
	return container.NewAppTabs(
		container.NewTabItem("有道翻译", container.NewPadded(makeYouDao())),
		container.NewTabItem("OpenAI", container.NewPadded(makeOpenAI())),
	)
}

func makeYouDao() *fyne.Container {

	youdaoDict := config.Conf.YouDaoDict

	appIdEntry := widget.NewEntry()
	appIdEntry.SetPlaceHolder("请输入应用ID")
	appIdEntry.SetText(youdaoDict.AppID)
	secretKeyEntry := widget.NewEntry()
	secretKeyEntry.SetPlaceHolder("请输入应用密钥")
	secretKeyEntry.SetText(youdaoDict.SecretKey)
	saveBtn := widget.NewButton("同步配置", func() {
		config.Conf.YouDaoDict.AppID = appIdEntry.Text
		config.Conf.YouDaoDict.SecretKey = secretKeyEntry.Text
	})
	return container.New(layout.NewFormLayout(),
		widget.NewLabel("应用ID"),
		appIdEntry,
		widget.NewLabel("应用Key"),
		secretKeyEntry,
		widget.NewLabel(""), saveBtn,
	)
}
func makeOpenAI() *fyne.Container {
	openAI := config.Conf.OpenAI
	urlEntity := widget.NewEntry()
	urlEntity.Text = openAI.Url
	modelNameEnty := widget.NewEntry()
	modelNameEnty.Text = openAI.ModelName
	apiKeyEnty := widget.NewEntry()
	apiKeyEnty.Text = openAI.ApiKey
	systemPromptEntry := widget.NewEntry()
	systemPromptEntry.Text = openAI.Prompt
	saveBtn := widget.NewButton("同步配置OpenAI", func() {
		config.Conf.OpenAI.Url = urlEntity.Text
		config.Conf.OpenAI.ModelName = modelNameEnty.Text
		config.Conf.OpenAI.ApiKey = apiKeyEnty.Text
		config.Conf.OpenAI.Prompt = systemPromptEntry.Text
	})

	return container.New(layout.NewFormLayout(),
		widget.NewLabel("URL"),
		urlEntity,
		widget.NewLabel("模型名称"),
		modelNameEnty,
		widget.NewLabel("apiKey"),
		apiKeyEnty,
		widget.NewLabel("系统提示词"),
		systemPromptEntry,
		widget.NewLabel(""), saveBtn,
	)
}
